package com.orange.core.service.banner.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.service.banner.BannerService;
import com.orange.core.mapper.banner.BannerMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.banner.Banner;
import com.orange.core.pojo.entity.banner.dto.BannerDTO;
import com.orange.core.pojo.entity.banner.vo.BannerVO;
import com.orange.core.pojo.req.banner.BannerPageReq;
import com.orange.core.util.ConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
* @author lwm
* @description 针对表【banner(Banner管理表)】的数据库操作Service实现
* @createDate 2025-06-02 23:20:07
*/
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner>
    implements BannerService {

    @Override
    public Page<BannerVO> page(PageReqDto<BannerPageReq> pageReqDto) {
        String title = Optional.ofNullable(pageReqDto.getCondition()).map(BannerPageReq::getTitle).orElse(null);
        Page<Banner> page = this.lambdaQuery().like(StrUtil.isNotBlank(title), Banner::getTitle, title)
                .orderByAsc(Banner::getSortOrder)
                .page(pageReqDto.toPage());
        Page<BannerVO> bannerVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(), page.getTotal());
        List<BannerVO> bannerVOS = ConvertUtils.convertListToVo(page.getRecords(), BannerVO.class);
        bannerVOPage.setRecords(bannerVOS);
        return bannerVOPage;
    }

    @Override
    public BannerVO info(Integer id) {
        Banner banner = this.getById(id);
        Assert.notNull(banner,"轮播图不存在");
        BannerVO bannerVO = new BannerVO();
        BeanUtil.copyProperties(banner,bannerVO);
        return bannerVO;
    }


    @Override
    @Transactional
    public boolean save(BannerDTO bannerDTO) {
        Banner banner = new Banner();
        BeanUtil.copyProperties(bannerDTO,banner);
        boolean result = this.save(banner);
        return result;
    }

    @Override
    @Transactional
    public boolean updateById(BannerDTO bannerDTO) {
        Banner banner = new Banner();
        BeanUtil.copyProperties(bannerDTO,banner);
        boolean result = this.updateById(banner);
        return result;
    }

    @Override
    @Transactional
    public boolean deleteBannerByIds(Integer[] ids) {
        boolean result = this.removeBatchByIds(Arrays.asList(ids));
        return result;
    }

    @Override
    public List<BannerVO> getBannerList() {
        List<Banner> list = this.list();
        List<BannerVO> bannerVOS = ConvertUtils.convertListToVo(list, BannerVO.class);
        return bannerVOS;
    }
}




